﻿// SinglePlayerStartScreen.xaml.cs
//
// Copyright (c) 2007-2008 Mike Strobel
//
// This source code is subject to the terms of the Microsoft Reciprocal License (Ms-RL).
// For details, see <http://www.opensource.org/licenses/ms-rl.html>.
//
// All other rights reserved.

using System.Linq;
using System.Windows;
using System.Windows.Documents;

using Supremacy.Entities;
using Supremacy.Game;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for SinglePlayerStartScreen.xaml
    /// </summary>
    public partial class SinglePlayerStartScreen
    {
        #region Constructors and Finalizers
        public SinglePlayerStartScreen()
        {
            InitializeComponent();

            GameContext.PushThreadContext(new GameContext());

            try
            {
                this.DataContext = CivDatabase.Load()
                    .Where(civ => civ.CivilizationType == CivilizationType.Empire)
                    .ToList();
            }
            finally
            {
                GameContext.PopThreadContext();
            }
        }
        #endregion

        #region Properties and Indexers
        public int EmpireID
        {
            get { return CivSelector.SelectedIndex; }
        }

        public GameOptions Options
        {
            get { return OptionsPanel.Options; }
        }
        #endregion

        #region Private Methods
        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            GameOptionsManager.SaveDefaults(OptionsPanel.Options);
            this.DialogResult = true;
        }
        #endregion
    }

    public static class BindableExtender
    {
        #region Constants
        public static readonly DependencyProperty BindableTextProperty =
            DependencyProperty.RegisterAttached(
                "BindableText",
                typeof(string),
                typeof(BindableExtender),
                new UIPropertyMetadata(
                    null,
                    BindableTextProperty_PropertyChanged));
        #endregion

        #region Public and Protected Methods
        public static string GetBindableText(DependencyObject obj)
        {
            return (string)obj.GetValue(BindableTextProperty);
        }

        public static void SetBindableText(
            DependencyObject obj,
            string value)
        {
            obj.SetValue(BindableTextProperty, value);
        }
        #endregion

        #region Private Methods
        private static void BindableTextProperty_PropertyChanged(
            DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs e)
        {
            if (dependencyObject is Run)
            {
                ((Run)dependencyObject).Text = (string)e.NewValue;
            }
        }
        #endregion
    }
}

//public class ClippingBorder : Border
//{
//    static ClippingBorder()
//    {
//        ClipProperty.OverrideMetadata(
//            typeof(ClippingBorder),
//            new FrameworkPropertyMetadata(
//                null,
//                CoerceClip));
//        BorderThicknessProperty.OverrideMetadata(
//            typeof(ClippingBorder),
//            new FrameworkPropertyMetadata((d, e) => d.CoerceValue(ClipProperty)));
//        CornerRadiusProperty.OverrideMetadata(
//            typeof(ClippingBorder),
//            new FrameworkPropertyMetadata((d, e) => d.CoerceValue(ClipProperty)));
//    }

//    public override UIElement Child
//    {
//        get
//        {
//            return base.Child;
//        }
//        set
//        {
//            ClearValue(ClipProperty);
//            base.Child = value;
//            CoerceValue(ClipProperty);
//        }
//    }

//    protected override Size ArrangeOverride(Size finalSize)
//    {
//        CoerceValue(ClipProperty);
//        return base.ArrangeOverride(finalSize);
//    }

//    private static object CoerceClip(DependencyObject d, object value)
//    {
//        if (DependencyPropertyHelper.GetValueSource(d, ClipProperty).BaseValueSource == BaseValueSource.Local)
//            return value;
//        var clippingBorder = d as ClippingBorder;
//        if ((clippingBorder == null) || (clippingBorder.Child == null) || clippingBorder.RenderSize.IsEmpty)
//            return value;
//        clippingBorder.InvalidateVisual();
//        return CreateClipGeometry(clippingBorder);
//    }

//    private static Geometry CreateClipGeometry(Border clippingBorder)
//    {
//        var clipGeometry = new StreamGeometry();
//        using (var geometryContext = clipGeometry.Open())
//        {
//            var cornerRadius = clippingBorder.CornerRadius;
//            var borderThickness = clippingBorder.BorderThickness;
//            var clipWidth = clippingBorder.ActualWidth - borderThickness.Left - borderThickness.Right;
//            var clipHeight = clippingBorder.ActualHeight - borderThickness.Top - borderThickness.Bottom;
//            clipGeometry.Transform = new TranslateTransform(borderThickness.Left, borderThickness.Top);
//            geometryContext.BeginFigure(
//                new Point(cornerRadius.TopLeft, 0),
//                true,
//                true);
//            geometryContext.LineTo(
//                new Point(clipWidth - cornerRadius.TopRight, 0),
//                false,
//                true);
//            geometryContext.ArcTo(
//                new Point(clipWidth, cornerRadius.TopRight),
//                new Size(cornerRadius.TopRight, cornerRadius.TopRight),
//                0,
//                false,
//                SweepDirection.Clockwise,
//                false,
//                true);
//            geometryContext.LineTo(
//                new Point(clipWidth, clipHeight - cornerRadius.BottomRight),
//                false,
//                true);
//            geometryContext.ArcTo(
//                new Point(clipWidth - cornerRadius.BottomRight, clipHeight),
//                new Size(cornerRadius.BottomRight, cornerRadius.BottomRight),
//                0,
//                false,
//                SweepDirection.Clockwise,
//                false,
//                true);
//            geometryContext.LineTo(
//                new Point(cornerRadius.BottomLeft, clipHeight),
//                false,
//                true);
//            geometryContext.ArcTo(
//                new Point(0, clipHeight - cornerRadius.BottomLeft),
//                new Size(cornerRadius.BottomLeft, cornerRadius.BottomLeft),
//                0,
//                false,
//                SweepDirection.Clockwise,
//                false,
//                true);
//            geometryContext.LineTo(
//                new Point(0, cornerRadius.TopLeft),
//                false,
//                true);
//            geometryContext.ArcTo(
//                new Point(cornerRadius.TopLeft, 0),
//                new Size(cornerRadius.TopLeft, cornerRadius.TopLeft),
//                0,
//                false,
//                SweepDirection.Clockwise,
//                false,
//                true);
//        }
//        return clipGeometry;
//        //var rectangle = new StreamGeometry();
//        //using (var dc = rectangle.Open())
//        //{
//        //    dc.BeginFigure(new Point(0, 0), true, true);
//        //    dc.LineTo(new Point(clippingBorder.ActualWidth, 0), false, true);
//        //    dc.LineTo(new Point(clippingBorder.ActualWidth, clippingBorder.ActualHeight), false, true);
//        //    dc.LineTo(new Point(0, clippingBorder.ActualHeight), false, true);
//        //    dc.Close();
//        //}
//        //return Geometry.Combine(
//        //    rectangle,
//        //    geometry,
//        //    GeometryCombineMode.Exclude,
//        //    null);
//    }
//}